﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Configuration
{
    public class EncryptedConfigurationSource : IConfigurationSource
    {
        public class EncryptedConfigurationSource : IConfigurationSource
        {
            private readonly string _filePath;
            private readonly string _encryptionKey;

            public EncryptedConfigurationSource(string filePath, string encryptionKey = null)
            {
                _filePath = filePath;
                _encryptionKey = encryptionKey;
            }

            public IConfigurationProvider Build(IConfigurationBuilder builder)
            {
                // 确保文件存在
                var fileProvider = builder.GetFileProvider();
                var fileInfo = fileProvider.GetFileInfo(_filePath);
                if (!fileInfo.Exists)
                {
                    throw new FileNotFoundException($"Configuration file not found: {_filePath}");
                }

                return new EncryptedConfigurationProvider(_filePath, _encryptionKey);
            }
        }
    }
}
